//
//  MineMenuView.m
//  UniversalApp
//
//  Created by 志刚杨 on 2018/1/28.
//  Copyright © 2018年 voidcat. All rights reserved.
//

#import "MineMenuView.h"
#import "MenuSubView.h"
#import "MineLogic.h"
#define tagStart 3000
@implementation MineMenuView
{
    QMUIGridView *contentView;
}

-(void)setTipArr:(NSArray *)tipArr
{
    _tipArr = tipArr;
    [contentView removeFromSuperview];
    [self contentView];
    [self addSubview:contentView];
}

-(QMUIGridView *)contentView
{
    NSArray *imgarArr = @[@"mine_certification",@"mine_privatePhoto",@"mine_inviteFriend",@"me_icon_divided",@"mine_level",@"mine_cooperation",@"mine_setting"];
    NSArray *titleArr = MENU_ITEM;
    if(_tipArr == nil)
    {
        _tipArr = @[ASLocalizedString(@"未认证"),@"",@"",@"",@"",@"",@"",@"",@""];
    }
    contentView = [[QMUIGridView alloc] initWithColumn:3 rowHeight:80];
    contentView.frame = self.bounds;
    
    for (int i=0; i<imgarArr.count; i++) {
        contentView.separatorWidth = 0;
        if([titleArr[i] isEqualToString:ASLocalizedString(@"免打扰")])
        {
            //视频认证
            MenuSubView *view1 = [MenuSubView initWithImage:IMAGE_NAMED(@"cyemptyimage") AndTitle:titleArr[i] AndTip:_tipArr[i]];
            view1.clickButton.tag = tagStart + i;
            view1.clickButton.frame = CGRectMake(0, 0, SCREEN_WIDTH/3, 80);
            [view1.clickButton addTarget:self action:@selector(handleClickEvent:) forControlEvents:UIControlEventTouchUpInside];
//            view1.layer.borderWidth = 0.3f;
//            view1.layer.borderColor = kGrayColor.CGColor;
            
            [contentView addSubview:view1];
            
            [view1 addSubview:self.blockSwitch];
            self.blockSwitch.frame = CGRectMake(0, 0, SCREEN_WIDTH/3, 80);
            [self.blockSwitch mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.equalTo(view1);
                make.centerY.equalTo(view1).offset(-16);
            }];
            self.blockSwitch.tag = tagStart + i;
//            return contentView;
        }
        else
        {
            //视频认证
            MenuSubView *view1 = [MenuSubView initWithImage:IMAGE_NAMED(imgarArr[i]) AndTitle:titleArr[i] AndTip:_tipArr[i]];
            view1.clickButton.tag = tagStart + i;
            view1.clickButton.frame = CGRectMake(0, 0, SCREEN_WIDTH/3, 80);
            [view1.clickButton addTarget:self action:@selector(handleClickEvent:) forControlEvents:UIControlEventTouchUpInside];
//            view1.layer.borderWidth = 0.3f;
//            view1.layer.borderColor = kGrayColor.CGColor;
            [contentView addSubview:view1];
        }
    }
    
    return contentView;
}


-(UISwitch *)blockSwitch
{
    if (_blockSwitch == nil) {
        _blockSwitch = [[UISwitch alloc]init];
        _blockSwitch.onTintColor = RGBCOLOR(248, 83, 170);
        [_blockSwitch addTarget:self action:@selector(blockSwitchChanged:) forControlEvents:UIControlEventValueChanged];
    }
    _blockSwitch.on = curUser.is_open_do_not_disturb;
    return _blockSwitch;
}
-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self)
    {
        [self contentView];
        [self addSubview:contentView];
    }
    return self;
}

-(void)handleClickEvent:(UIButton *)button
{
    long index = button.tag -  tagStart;
    if(self.itemClick)
    {
        self.itemClick(index);
    }
}

-(void)blockSwitchChanged:(id)sender
{
    UISwitch *switchButton = (UISwitch*)sender;
    BOOL isButtonOn = [switchButton isOn];
    
    //不回调说明请求错误
    [MineLogic requestSetDoNotDisturtWithBock:^(id selfPtr) {
        
    }];
    
    if (isButtonOn) {
        NSLog(ASLocalizedString(@"开"));
    }else {
        NSLog(ASLocalizedString(@"关"));
    }
}


@end
